package com.example.wan_android.ui.activity

import android.text.Editable
import android.text.TextWatcher
import com.bumptech.glide.Glide
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.databinding.ActivityLoginBinding
import com.example.wan_android.viewmodel.LoginViewModel

class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>(
    LoginViewModel::class.java,
    R.layout.activity_login,
    BR.viewModel
) {


    override fun initView() {
        mViewModel?.context = this
        mViewModel?.showPassword?.observe(this) {
            if (it) {
                Glide.with(this).load(R.drawable.ic_un_display).into(mViewDataBinding?.ivDisplay!!)
            } else {
                Glide.with(this).load(R.drawable.display).into(mViewDataBinding?.ivDisplay!!)
            }
        }
        mViewModel?.currentPosition?.observe(this) {
            mViewDataBinding?.etPassword?.setSelection(it)
        }
        mViewDataBinding?.etPassword?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.setCurrentPosition(p0?.length ?: 0)
            }

        })

        mViewDataBinding?.btLogin?.setOnClickListener({
            mViewModel?.firstLogin(
                mViewDataBinding?.etNumber?.text.toString() ?: "",
                mViewDataBinding?.etPassword?.text.toString() ?: ""
            )
        })


    }
}